extern crate num;
extern crate image;
extern crate qrcode;

use std::fs::File;
use std::path::Path;
use std::env;

const SCALE: usize = 10usize;

fn main() {
    let arg = env::args().nth(1).unwrap();
    let code = qrcode::QrCode::new(arg.as_bytes()).unwrap();

    let size = (code.width() * SCALE) as u32;
    let mut imgbuf = image::ImageBuffer::new(size, size);

    for y in 0 .. code.width() {
        for x in 0 .. code.width() {
            let block = if code[(x as usize , y as usize)] {
                image::Rgb([0,0,0])
            } else {
                image::Rgb([255,255,255])
            };

            for zy in 0..SCALE {
                for zx in 0..SCALE {
                    let px = x * SCALE + zx;
                    let py = y * SCALE + zy;
                    *imgbuf.get_pixel_mut(px as u32, py as u32) = block;
                }
            }
        }
    }

    let ref mut fout = File::create(&Path::new("qr.png")).unwrap();
    let _    = image::ImageRgb8(imgbuf).save(fout, image::PNG);
}
